package stream.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

@RestController
public class StreamController {

    @GetMapping("/parallel/sleep")
    public void sleep() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 32; i++) {
            list.add(i);
        }
        ForkJoinPool forkJoinPool = new ForkJoinPool(32);
        forkJoinPool.submit(() -> list.parallelStream().forEach(i -> {
            System.out.println("parallel: " + i);
            System.out.println(Thread.currentThread().getId());
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        })).join();
    }

    @GetMapping("/parallel/execute")
    public void execute() {
        List<Integer> list = new ArrayList<>();
        for (int i = 1000; i < 1016; i++) {
            list.add(i);
        }
        ForkJoinPool forkJoinPool = new ForkJoinPool(2);
       list.parallelStream().forEach(i -> {
            System.out.println("***parallel: " + i);
            System.out.println("***parallel: " + Thread.currentThread().getId());
            try {
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
        });
    }
}
